<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		</head>
	<body>
		<s:hidden name="id" id="survey_id"></s:hidden>
		<!-- 问卷头部 -->
		<div class="widget-box">
			<div class="widget-title">
				<span class="icon"> <i class="icon-align-justify" ></i> </span>
				<h5>
					问卷设计
				</h5>
				<span class="label label-info">创建时间：<s:text name="createTime"></s:text></span>
			</div>
			<!-- 问卷设计区 -->
			<div class="widget-content">
				<div class="alert alert-block">
					<div class="error_ex">
						<h4 class="alert-heading">
							<s:text name="name"></s:text>
						</h4>
					</div>
				</div>
				<div class="alert  alert-success alert-block">
					<s:text name="preface"></s:text>
				</div>
				<div class="alert alert-error alert-block">
					<strong>问卷设计区</strong>
					<div class="wjsjq-button">
						<%--<a href="#"><strong>下载模板</strong></a>--%>
						<%--<a href="#" id="select_question" class="btn btn-primary btn-mini">从题库选择</a>--%>
						<a href="#" id="format_survey" class="btn btn-danger btn-mini">格式化</a>
						<a href="#" id="survey_submit" class="btn btn-success btn-mini">保存设计</a>
					</div>
					<div class="control-group">
						<div class="search-controls">
							<s:textarea cssStyle="margin:0px; width:95%; height:200px;"
								name="content" id="survey_content"></s:textarea>
						</div>
					</div>
				</div>
				<div class="alert  alert-info alert-block" style="margin-top: 0px;">
					<strong>问卷预览区</strong>
					<div class="todo top-black">
						<ul id="survey_view">
						</ul>
					</div>
				</div>
				<div class="alert  alert-success alert-block">
					<s:text name="afterword"></s:text>
				</div>
			</div>
		</div>
	</body>
		<link rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrap.min.css" />
		<link rel="stylesheet" href="<%=request.getContextPath()%>/css/matrix-style.css" />
		<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.min.js"></script>		
		<script type="text/javascript" src="<%=request.getContextPath()%>/js/lhgdialog/dialogUtil.js"></script>
		<script type="text/javascript" src="<%=request.getContextPath() %>/js/lhgdialog/lhgdialog.min.js?skin=iblue"></script>
		<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery/plugin/jquery.form.min.js"></script>
		<script type="text/javascript" src="<%=request.getContextPath()%>/js/comm/json2.js"></script>
		<script type="text/javascript" src="<%=request.getContextPath()%>/js/comm/textarea.js"></script>
		<link href="<%=request.getContextPath() %>/font-awesome/css/font-awesome.css" rel="stylesheet" />	
		<script type="text/javascript" >
		var queStart = /^[0-9]*[\、.]/;//试题开始标记
		var queStyle = /\[\s*题目类型\s*[:：][^\]]*\]/;//试题类型标记
		var queRequ = /\[\s*是否必答\s*[:：][^\]]*\]/;//试题必答标记
		var queFactor = /\[\s*问卷因子\s*[:：][^\]]*\]/;//问卷因子
		var queSpeMark = /\[\s*特殊标记\s*[:：][^\]]*\]/;//特殊标记
		var queSelectCounts = /\[\s*最多选择项\s*[:：][^\]]*\]/;//最多选择项
		var queKeyword = /\[\s*关键词\s*[:：][^\]]*\]/;//关键词

		var styStart = /\[\s*题目类型\s*[:：]/;//试题类型值开始标记
		var reqStart = /\[\s*是否必答\s*[:：]/;//是否必答值开始标记
		var facStart = /\[\s*问卷因子\s*[:：]/;//问卷因子开始标记
		var speMarkStart = /\[\s*特殊标记\s*[:：]/;//特殊开始标记
		var selectCountsStart = /\[\s*最多选择项\s*[:：]/;//最多选择项
		var keywordStart = /\[\s*关键词\s*[:：]/;//关键词

		var queOption = /[a-zA-Z][:：]/g;//试题选项标记
		var queAnswer = /^[答][:：]/g;//文本题答案开始标记
		var queOptionMark = /\[([0-9]{1,}|[N])分\]/;//分数标记
		var optValueEnd = /分[\]]/;//分值结束标记
		var endVar = /[\]]/;//通用]结束标记
		var beforVar = /^[\[]/;//通用[开始标记
		var defStyle='单选题';//默认试题类型
		var defRequ = '否';//默认是否必答

		/*定义题目类型*/
		var que_style_single = "单选题";
		var que_style_multiple = "复选题";
		var que_style_text = "文本题";

		/*定义试题属性key*/
		var que_name = 'name';
		var que_style= 'style';
		var que_required= 'required';
		var que_option= 'optionList';
		var que_factor= 'factor';
		var que_speMar = 'speMark';
		var que_selectCounts = 'selectCounts';
		var que_keyword = 'keyword';

		/*定义选项属性Key*/
		var opt_id = "name";
		var opt_name = "content";
		var opt_score = "score";

		//格式化-预览试题
		function viewSurvey(){
			var textContent = jQuery("#survey_content");
			var content = textContent.val();
			var qList = formatQuestion(content);
			jQuery("#survey_view li").remove();
			//alert(qList);
			for (var i = 0 ,j = qList.length; i < j ; i++){
				
				var li = jQuery('<li class="clearfix"></li>');
				var qDiv = jQuery('<div class="txt top5" style="width:100%"></div>');
				var qStr = jQuery("<span style='font-weight:bold;font-size:15px;'></span>");
				var qSpan = jQuery('<span class="label label-success"></span>');
				
				var qName = (i+1)+"、"+qList[i][que_name];
				var qType = "&nbsp;&nbsp;[题目类型："+qList[i][que_style]+"]&nbsp;&nbsp;[是否必答："+qList[i][que_required]+"]";
				if(qList[i][que_keyword]!=''){
					qType += "&nbsp;&nbsp;[关键词："+qList[i][que_keyword]+"]";
				}
				if(qList[i][que_factor]!=''){
					qType += "&nbsp;&nbsp;[问卷因子："+qList[i][que_factor]+"]";
				}
				if(qList[i][que_speMar]!=''){
					qType += "&nbsp;&nbsp;[特殊标记："+qList[i][que_speMar]+"]";
				}
				
				qSpan.append(qType);
				qStr.append(qName);
				qDiv.append(qStr).append(qSpan);
				
				var oDiv = jQuery('<div class="txt top5" style="width:100%"></div>');
				var oList = qList[i][que_option];
				
				for (var m = 0 ,n = oList.length ; m < n ; m++){
					var oName = oList[m][opt_id]+ oList[m][opt_name];
					var oLabel = jQuery('<label style="padding-left: 20px;"></label>');
					
					if (qList[i][que_style] == que_style_single){
						oLabel.append('<input type="radio" style="margin:0px 5px" name="radio'+i+'" />');
						oName += "["+oList[m][opt_score]+"分]";
					} else if (qList[i][que_style] == que_style_multiple){
						oLabel.append('<input type="checkbox" style="margin:0px 5px" name="radio'+i+'" />');
						oName += "["+oList[m][opt_score]+"分]";
					} else {
						//oLabel.append('<br/>');
					}
					
					oLabel.append(oName);
					oDiv.append(oLabel);
				}
				li.append(qDiv).append(oDiv);
				li.bind({"mouseover":function(){
					//textContent.selectContent("A：较有进步 [4分]");
					//textContent.focus();
					textContent.setCurPos(400);
				}})
				
				jQuery("#survey_view").append(li);
			}
		}



		jQuery(function(){
			
			viewSurvey();
			
			jQuery("#survey_content").keydown(viewSurvey);
			jQuery("#survey_content").keyup(viewSurvey);
			
			jQuery("#select_question").bind("click",function(){
				showDialog("选择试题",750,400,"<%=request.getContextPath() %>/wjgl/stgl/toSelectQuestion.html");
			});

			jQuery("#format_survey").bind("click",function(){
				confirm("是否格式化问卷？",function(){
					jQuery("#survey_content").val("");
					jQuery("#survey_view").html("");
					jQuery("#survey_content").focus();
				});
			});


			
			jQuery("#survey_submit").bind("click",function(){
				
				confirm("是否保存问卷设计？",function(){					
					var content = jQuery("#survey_content").val();
					var questionList = formatQuestion(content);
					var jsonData = {};
					jsonData["id"]=jQuery("#survey_id").val();
					jsonData["questionList"] = questionList;
					
					jQuery.post("<%=request.getContextPath() %>/wjgl/survey/saveDeginSurvey.html",
								{"jsonData":JSON.stringify(jsonData),
								 "content":content,
								 "id":jsonData["id"]
								},
								function(data){
									confirm(data,function(){
										closeDialog();
									});
								},
								'json');
				});
			});
		});

		//格式化试题
		function formatQuestion(content){
			var array = content.split("\n");
			var jsonArray = [];
			var jsonMap;
			
			for (var i = 0 , j = array.length ; i < j ; i++){
				var n = array[i];
				
				if (queStart.test(n)){
					//将试题放入集合
					if (jsonMap != undefined){
						jsonArray.push(jsonMap);
					}
					//解析试题属性
					jsonMap = {};
					jsonMap[que_name] = n.replace(queStart,'');
					jsonMap[que_option] = [];
				} else if (beforVar.test(n)){
					//试题类型、必填、特殊、因子等属性
					jsonMap[que_style] = formatQueStyle(n);//类型
					jsonMap[que_required] = formatQueRequ(n);//必填
					jsonMap[que_factor] = formatQueFactor(n);//因子
					jsonMap[que_speMar] = formatQueSpeMark(n);//特殊标记
					jsonMap[que_selectCounts] = formatQueSelectCounts(n);//最多选择项
					jsonMap[que_keyword] = formatQueKeyword(n);//关键词
				} else if (queOption.test(n) && (que_style_single == jsonMap[que_style] || que_style_multiple == jsonMap[que_style])){
					//解析试题答案
					formatOption(n,jsonMap[que_option]);
				} else if ((queAnswer.test(n) || (jsonMap!=undefined && jsonMap[que_option].length > 0)) && que_style_text == jsonMap[que_style]){
					//解析文本题
					var answer = {};
					
					if (jsonMap[que_option].length > 0){
						answer = jsonMap[que_option][0];
					}
					formatAnswer(n,answer);
					jsonMap[que_option][0] = answer;
				} else {
					
				}
			}
			if (jsonMap != undefined){
				jsonArray.push(jsonMap);
			}
			return jsonArray;
		}

		//格式化试题关键词
		function formatQueKeyword(n){
			if (queKeyword.test(n)){
				return n.match(queKeyword)[0].replace(keywordStart,'').replace(endVar,'').replace(/\s*/g,'');;
			}
			return '';
		}

		//格式化试题最多选择项
		function formatQueSelectCounts(n){
			if (queSelectCounts.test(n)){
				return n.match(queSelectCounts)[0].replace(selectCountsStart,'').replace(endVar,'').replace(/\s*/g,'');;
			}
			return 0;
		}

		//格式化试题特殊类型标记
		function formatQueSpeMark(n){
			if (queSpeMark.test(n)){
				return n.match(queSpeMark)[0].replace(speMarkStart,'').replace(endVar,'').replace(/\s*/g,'');;
			}
			return '否';
		}

		//格式化试题类型
		function formatQueStyle(n){
			if (queStyle.test(n)){
				return n.match(queStyle)[0].replace(styStart,'').replace(endVar,'').replace(/\s*/g,'');;
			}
			return defStyle;
		}

		//格式化是否必答
		function formatQueRequ(n){
			if (queRequ.test(n)){
				return n.match(queRequ)[0].replace(reqStart,'').replace(endVar,'').replace(/\s*/g,'');;
			}
			return defRequ;
		}

		//格式化问卷因子
		function formatQueFactor(n){
			if (queFactor.test(n)){
				return n.match(queFactor)[0].replace(facStart,'').replace(endVar,'').replace(/\s*/g,'');;
			}
			return '';
		}

		//格试化试题选项
		function formatOption(n,options){
			var optionArray = n.replace(/\s*/g,'').split(queOption);
			var tCount = 0;
			var ids = n.match(queOption);
			for (var i = 0 , n = optionArray.length; i < n ; i++){
				if (optionArray[i] != ''){
					var optionMap = {};
					optionMap[opt_id] = ids[tCount];
					optionMap[opt_name] = optionArray[i].replace(queOptionMark,'');
					optionMap[opt_score] = formatScore(optionArray[i]);
					options.push(optionMap);
					tCount++;
				}
			}
		}
		//文本题答案
		function formatAnswer(n , answer){
			answer[opt_id] = '答：';
			var tName = answer[opt_name]||'';
			answer[opt_name] = tName+n.replace(queAnswer,'');
			return answer;
		}


		//格式化分值
		function formatScore(n){
			if (queOptionMark.test(n)){
				return n.match(queOptionMark)[0].replace(beforVar,'').replace(optValueEnd,'');
			}
			return 0;
		}

		//插入试题
		function insertQuestion(ids){
			jQuery.post("<%=request.getContextPath() %>/wjgl/stgl/getSelectQuestion.html",{ids:ids},function(data){
				
				var textContent = "\n";
				
				jQuery.each(data,function(i,n){
					textContent +=(i+1)+"、";
					textContent +=n["name"];
					textContent +="\n";
					textContent +="[题目类型："+getQtype(n["styleId"])+"]";
					textContent +="[是否必答："+(n["required"] == "0" ? "是" : "否")+"]";
					textContent +="[问卷因子："+(n["factor"]["name"])+"]";
					textContent +="[关键词："+n["keyword"]+"]";
					textContent +="\n";
					
					jQuery.each(n["optionList"],function(x,y){
						textContent += y["name"];
						textContent += y["content"];
						textContent += "["+y["score"]+"分]";
						textContent +="\n";
					});
					
				});
				
				jQuery("#survey_content").append(textContent);
				viewSurvey();
			},'json');
		}


		function getQtype(styleId){
			if (styleId == "1"){
				return "单选题";
			}
			
			if (styleId == "2"){
				return "复选题";
			}
			
			if (styleId == "3"){
				return "文本题";
			}
		}
		</script>
</html>
